﻿namespace Bildverarbeitung.CGFilter
{
    using System;
    using System.Drawing;
    using Bildverarbeitung;
    using AForge.Imaging.Filters;
    using AForge.Imaging;

    /// <summary>
    /// 
    /// </summary>
    public class RichardtManuelFilter : Bildverarbeitung.IFilter
    {
        /// <summary>
        /// Gets the summerized difference from average of all images.
        /// </summary>
        /// <param name="images">Given images.</param>
        /// <returns>The summerized difference.</returns>
        public Bitmap GetSummerizedDifferencefromAverage(Bitmap[] images)
        {
            Bitmap pic = new Bitmap(images[0].Width, images[0].Height);
            Bitmap newImage = images[0]; ;

            for (int i = 0; i < images.Length - 2; i++)
            {
                ThresholdedDifference filter = new ThresholdedDifference(50);
                filter.OverlayImage = newImage;
                pic = filter.Apply(images[i + 1]);

                AForge.Imaging.BlobCounter blobCounter = new BlobCounter(pic);
                Rectangle[] rects = blobCounter.GetObjectsRectangles();

                Graphics g = Graphics.FromImage(newImage);
                TextureBrush textureBrush = new TextureBrush(images[i + 2]);
                g.FillRectangles(textureBrush, rects);
            }
            return newImage;
        }

        /// <summary>
        /// Return the author´s name
        /// Please use NachnameVorname (without spaces).
        /// </summary>
        /// <returns>author´s name</returns>
        public String GetName()
        {
            return ("RichardtManuel");
        }
    }
}
